<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Escape verify</title>
    <link rel="stylesheet" href="./lib/main.css" />
    <link rel="stylesheet" href="./lib/bootstrap/css/bootstrap.min.css" />
    <script src="./lib/GoogleAnalytics.js"></script>
    <script src="./lib/crypto-js.js"></script>
    <script src="./lib/tools.js"></script>
    <script src="./lib/vue3.js"></script>
    <style>
      [v-cloak] {
        display: none !important;
      }

      .result-content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }

      .result {
        display: table-cell;
        padding: 0.5em 1em;
        width: 2em;
        height: 2em;
        font-size: 1.5em;
      }
    </style>
  </head>

  <body>
    <div id="app" class="main" v-cloak>
      <h1 class="text-center pb-5">Escape verify</h1>
      <hr />
      <form class="py-5">
        <h2 class="text-center">Input</h2>
        <div class="form-group">
          <input
            :value="hash"
            @change="hash = $event.target.value"
            class="form-control"
            placeholder="Hash"
          />
        </div>
        <div class="form-group">
          <input
            :value="salt"
            @change="salt = $event.target.value"
            class="form-control"
            placeholder="Salt"
          />
        </div>
      </form>
      <h2 class="text-center">Result</h2>
      <div class="text-center">
         <template v-for="(item,i) in result" :key="i">
          <span v-if="i===0" style="color: #49a1fe;">{{item}}</span>
          <span v-if="i===1" style="color: #208730;">{{item}}</span>
          <span v-if="i===2" style="color: #b91414;">{{item}}</span>
         </template>
      </div>
    </div>

    <script>
      const { createApp, reactive, toRefs, ref, onMounted, watch } = Vue;

      const app = createApp({
        setup() {
          const RTP = 99.0;
          const CardsNum = 25;
          const salt = ref(
            "00000000029c70bc32d1c9d06ce111871b552abc84cdc50bc196454039363049"
          );

          const hash = ref("");
          const result = ref([]);

          onMounted(() => {
            let qs = tools.queryString();
            let h = qs.s || "";
            if (h != "") {
              hash.value = h;
              result.value = minesGetResult(h);
            }
          });

          const minesGetResult = (hash) => {
           
            let killerNumber = 3;
            let roomNumber = 6;
            let nonceIncrease = 0;
            const chars = 2;
            const maxNumber =
              Math.floor(Math.pow(16, chars) / roomNumber) * roomNumber;
            const finalIndexes = [];
            const finalIndexesMap = new Set();
            let done = false;

            while (!done) {
              const hmac = CryptoJS.HmacSHA256(
                `${hash}-${nonceIncrease}`,
                salt.value
              );
              const seed = hmac.toString(CryptoJS.enc.Hex);

              for (let i = 0; i < Math.floor(128 / chars); i++) {
                const tmp = seed.substring(i * chars, i * chars + chars);
                const number = parseInt(tmp, 16);

                if (!isNaN(number) && number < maxNumber) {
                  const remainder = (number % roomNumber) + 1;

                  if (!finalIndexesMap.has(remainder)) {
                    finalIndexes.push(remainder);
                    finalIndexesMap.add(remainder);
                    if (finalIndexes.length == killerNumber) {
                      done = true;
                      break;
                    }
                  }
                }
              }
              nonceIncrease++;
            }
            return finalIndexes;
          };

          const shuffle = (array) => {
            for (let i = array.length - 1; i > 0; i--) {
              const j = Math.floor(Math.random() * (i + 1));
              [array[i], array[j]] = [array[j], array[i]];
            }
            return array;
          };

          const getResult = (arr, num) => {
            if (!arr) {
              return;
            }
            return arr.some((e) => e == num);
          };

          watch(
            () => [hash.value],
            async ([newHash], [oldHash]) => {
              const res = minesGetResult(newHash);
              result.value = res;
            }
          );

          return {
            hash,
            salt,
            result,
            minesGetResult,
            shuffle,
            getResult,
          };
        },
      });
      app.mount("#app");
    </script>
  </body>
</html>
